package com.yuelife.service.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuelife.service.dao.SOrderMapper;
import com.yuelife.service.pojo.SOrder;
import com.yuelife.service.service.SOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:admin
 * @Description:SOrder业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class SOrderServiceImpl implements SOrderService {

    @Autowired
    private SOrderMapper sOrderMapper;


    /**
     * SOrder条件+分页查询
     * @param sOrder 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<SOrder> findPage(SOrder sOrder, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(sOrder);
        //执行搜索
        return new PageInfo<SOrder>(sOrderMapper.selectByExample(example));
    }

    /**
     * SOrder分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<SOrder> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<SOrder>(sOrderMapper.selectAll());
    }

    /**
     * SOrder条件查询
     * @param sOrder
     * @return
     */
    @Override
    public List<SOrder> findList(SOrder sOrder){
        //构建查询条件
        Example example = createExample(sOrder);
        //根据构建的条件查询数据
        return sOrderMapper.selectByExample(example);
    }


    /**
     * SOrder构建查询对象
     * @param sOrder
     * @return
     */
    public Example createExample(SOrder sOrder){
        Example example=new Example(SOrder.class);
        Example.Criteria criteria = example.createCriteria();
        if(sOrder!=null){
            // 自增长订单id)
            if(!StringUtils.isEmpty(sOrder.getOrderId())){
                    criteria.andEqualTo("orderId",sOrder.getOrderId());
            }
            // 用户id
            if(!StringUtils.isEmpty(sOrder.getUseId())){
                    criteria.andEqualTo("useId",sOrder.getUseId());
            }
            // 提交时间
            if(!StringUtils.isEmpty(sOrder.getCreateTime())){
                    criteria.andEqualTo("createTime",sOrder.getCreateTime());
            }
            // 用户名称
            if(!StringUtils.isEmpty(sOrder.getUsename())){
                    criteria.andLike("usename","%"+sOrder.getUsename()+"%");
            }
            // 订单状态：0->待付款；1->待接单；2->已接单；3->已完成；4->已关闭；5->无效订单
            if(!StringUtils.isEmpty(sOrder.getStatus())){
                    criteria.andEqualTo("status",sOrder.getStatus());
            }
            // 订单总金额
            if(!StringUtils.isEmpty(sOrder.getTotalAmount())){
                    criteria.andEqualTo("totalAmount",sOrder.getTotalAmount());
            }
            // 实际支付金额
            if(!StringUtils.isEmpty(sOrder.getPayAmount())){
                    criteria.andEqualTo("payAmount",sOrder.getPayAmount());
            }
            // 地址
            if(!StringUtils.isEmpty(sOrder.getAddress())){
                    criteria.andEqualTo("address",sOrder.getAddress());
            }
            // 商家id
            if(!StringUtils.isEmpty(sOrder.getStoreId())){
                    criteria.andEqualTo("storeId",sOrder.getStoreId());
            }
            // 收货人姓名
            if(!StringUtils.isEmpty(sOrder.getGetName())){
                    criteria.andEqualTo("getName",sOrder.getGetName());
            }
            // 收货人电话
            if(!StringUtils.isEmpty(sOrder.getGetMobile())){
                    criteria.andEqualTo("getMobile",sOrder.getGetMobile());
            }
            // 备注
            if(!StringUtils.isEmpty(sOrder.getNote())){
                    criteria.andEqualTo("note",sOrder.getNote());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        sOrderMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改SOrder
     * @param sOrder
     */
    @Override
    public void update(SOrder sOrder){
        sOrderMapper.updateByPrimaryKey(sOrder);
    }

    /**
     * 增加SOrder
     * @param sOrder
     */
    @Override
    public void add(SOrder sOrder){
        sOrderMapper.insert(sOrder);
    }

    /**
     * 根据ID查询SOrder
     * @param id
     * @return
     */
    @Override
    public SOrder findById(Integer id){
        return  sOrderMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询SOrder全部数据
     * @return
     */
    @Override
    public List<SOrder> findAll() {
        return sOrderMapper.selectAll();
    }

    /**
     * 查指定商家的订单
     * @param storeId
     * @return
     */
    @Override
    public List<SOrder> selectSOrderByStoreId(Integer storeId) {
        return sOrderMapper.selectSOrderByStoreId(storeId);
    }

    /**
     * 多条件查订单和其明细
     * @param sOrder
     * @return
     */
    @Override
    public List<SOrder> selectbySOrder(SOrder sOrder) {
        return sOrderMapper.selectbySOrder(sOrder);
    }

    /**
     * 取消订单
     * @param orderId
     */
    @Override
    public void updateStatus(Integer orderId) {
        sOrderMapper.updateStatus(orderId);
    }

    /**
     * 确认收货
     * @param orderId
     */
    @Override
    public void updateStatusConfirm(Integer orderId) {
        sOrderMapper.updateStatusConfirm(orderId);
    }

    /**
     * 付款成功
     * @param orderId
     */
    @Override
    public void updateStatuspay(Integer orderId) {
        sOrderMapper.updateStatuspay(orderId);
    }


}
